package Thread2;

// 通过饿汉模式编写
class Singleton2 {
    private static Singleton2 instance = new Singleton2();

    public static Singleton2 getInstance(){
        return instance;
    }

    private Singleton2(){

    }
}

// 通过懒汉模式编写
class SingletonLazy3{
    private static volatile SingletonLazy3 instance = null;
    private static Object locker = new Object();
    public static SingletonLazy3 getInstance(){
        if(instance == null){
            synchronized (locker){
                if(instance == null){
                    instance = new SingletonLazy3();
                }
            }
        }
        return instance;
    }

    private SingletonLazy3(){

    }
}
public class Homework5 {
    public static void main(String[] args) {
        Singleton2 s1 = Singleton2.getInstance();
        Singleton2 s2 = Singleton2.getInstance();
        System.out.println(s1 == s2);

        SingletonLazy3 s3 = SingletonLazy3.getInstance();
        SingletonLazy3 s4= SingletonLazy3.getInstance();
        System.out.println(s3 == s4);
    }
}
